<template>
	<view>
		<navbar title="添加消息" :isbg="false" :autoBack="true" :opacity='1'></navbar>
		<view class="con">
			<view class="titleinp">
				<view class="label">
					标题:
				</view>
				<input type="text" class="inp" v-model="form.title" />
			</view>
			<view class="box">
				<view class="box__a" @click="clicktype">
					<view class="label">
						类型:
					</view>
					<view class="inp" style="margin-right: 6rpx;">
						{{form.type}}
					</view>
					<u-icon name="arrow-down"></u-icon>
				</view>
				<view class="box__t">
					<view class="box__t__line">

					</view>
					接收人
				</view>
				<view class="u-flex u-flex-y-center u-flex-wrap">
					<view class="box__i" v-for="(item,index) in qtlist" :key="'qt'+index"
						@click="item.ischeck=!item.ischeck">
						{{item.name}}
						<view class="box__i__check" v-if="item.ischeck">
							<u-icon name="checkmark" color="#fff" size="22rpx"></u-icon>
						</view>
						<view class="box__i__nocheck" v-else>
					
						</view>
					</view>
					<view class="box__i" v-for="(item,index) in bmlist" :key="'bm'+index"
						@click="item.ischeck=!item.ischeck,$forceUpdate()">
						{{item.name}}
						<view class="box__i__check" v-if="item.ischeck">
							<u-icon name="checkmark" color="#fff" size="22rpx"></u-icon>
						</view>
						<view class="box__i__nocheck" v-else>

						</view>
					</view>
					<view class="box__i" v-for="(item,index) in yglist" :key="'yg'+index"
						@click="item.ischeck=!item.ischeck,$forceUpdate()">
						{{item.username}}
						<view class="box__i__check" v-if="item.ischeck">
							<u-icon name="checkmark" color="#fff" size="22rpx"></u-icon>
						</view>
						<view class="box__i__nocheck" v-else>
					
						</view>
					</view>
				</view>
				<view class="" style="padding-right: 20rpx;">
					<u--textarea v-model="form.content" height='200rpx' placeholder="请输入详情信息" border="none"></u--textarea>
				</view>
				<u-button text="提交" @click="submit" :loading="btnloading" color="#2A51FA" :customStyle="{
					height:'95rpx',
					width:'638rpx',
					margin:0,
					fontSize:'30rpx',
					borderRadius:'20rpx'
				}"></u-button>
			</view>
		</view>
		<u-picker :show="show" :columns="type" :loading='loading' closeOnClickOverlay @close='show=false' @cancel='show=false' @confirm='confirm'></u-picker>
	</view>
</template>

<script>
	import datagl from '@/api/_datagl.js'
	export default {
		data() {
			return {
				value5: '',
				show: false,
				loading:false,
				type: [],
				qtlist:[{name:'全体',ischeck:true}],
				bmlist:[],
				yglist:[],
				form:{
					user_id:[],
					type:'通知',
					title:'',
					content:'',
					isAll:1
				},
				btnloading:false
			};
		},
		methods:{
			confirm(e){
				// console.log(e)
				this.show=false
				this.form.type=e.value[0]
			},
			async clicktype() {
				this.loading=true
				this.show=true
				let res = await datagl.readConfig({
					configName: 'msgtype'
				})
				if (res.data.code == 1) {
					this.type=[res.data.data.msgtype]
					this.loading=false
				}
			},
			submit(){
				let {title,content} = this.form
				if(!title){
					uni.$u.toast('请输入标题')
					return
				}
				if(!content){
					uni.$u.toast('请输入内容')
					return
				}
				this.btnloading=true
				// 判断是否全体
				if(this.qtlist[0].ischeck){//全体
					this.tj()
				}else{
					this.form.isAll=2
					let userarr=[]
					let bmarr=[]
					this.bmlist.map(t=>{
						if(t.ischeck){
							t.users.map(t1=>{
								userarr.push(t1.id)
							})
						}
					})
					this.yglist.map(t=>{
						if(t.ischeck){
							userarr.push(t.id)
						}
					})
					this.form.user_id=this.unique(userarr)
					this.tj()
				}
			},
			async tj(){
				let res = await datagl.createNotify(this.form)
				if(res.data.code==1){
					
					this.btnloading=false
					const pages = getCurrentPages(); // 获取页面栈
					const beforePage = pages[pages.length - 2]; // 上一个页面
					beforePage.$vm.getlist(beforePage.$vm.list4[0].name); // 调用上一个页面的方法
					uni.navigateBack()
				}
			},
			unique(arr) {
			  return arr.filter(function(item, index, arr) {
			    //当前元素，在原始数组中的第一个索引==当前索引值，否则返回当前元素
			    return arr.indexOf(item, 0) === index;
			  });
			},
			async clickbm(){
				let res = await datagl.getDepartList()
				if(res.data.code==1){
					this.bmlist=res.data.data
					this.bmlist.map(t=>{
						t.ischeck=false
					})
				}
			},
			async getyg(){
				let res = await datagl.getUserList({depart_id:''})
				if(res.data.code==1){
					this.yglist=res.data.data
					this.yglist.map(t=>{
						t.ischeck=false
					})
				}
			}
		},
		onLoad() {
			this.clickbm()
			this.getyg()
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #F8F8F8;
	}

	.con {
		padding: 30rpx;

		.titleinp {
			height: 92rpx;
			background: #FFFFFF;
			border-radius: 20rpx 20rpx 20rpx 20rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 0 30rpx 0 26rpx;
			margin-bottom: 24rpx;
		}

		.label {
			font-family: PingFang-SC, PingFang-SC;
			font-weight: 500;
			font-size: 28rpx;
			color: #6D6D6D;
			line-height: 28rpx;
		}

		.inp {
			font-family: PingFang-SC, PingFang-SC;
			font-weight: 500;
			font-size: 28rpx;
			color: #020202;
			line-height: 28rpx;
			text-align: right;
			flex: 1;
		}

		.box {
			background: #FFFFFF;
			border-radius: 20rpx 20rpx 20rpx 20rpx;
			padding: 0 8rpx 43rpx 24rpx;

			&__a {
				height: 91rpx;
				border-bottom: 2rpx solid #F9F9F9;
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding-right: 20rpx;
			}

			&__t {
				font-family: PingFang-SC, PingFang-SC;
				font-weight: 500;
				font-size: 28rpx;
				color: #6D6D6D;
				line-height: 28rpx;
				display: flex;
				align-items: center;
				margin: 36rpx 0 20rpx;

				&__line {
					width: 6rpx;
					height: 27rpx;
					background: #2DA1E0;
					border-radius: 3rpx 3rpx 3rpx 3rpx;
					margin-right: 10rpx;
				}
			}

			&__i {
				width: 158rpx;
				height: 80rpx;
				background: #F8F8F8;
				border-radius: 10rpx 10rpx 10rpx 10rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 0 20rpx;
				margin: 0 20rpx 20rpx 0;
				font-family: PingFang-SC, PingFang-SC;
				font-weight: bold;
				font-size: 22rpx;
				color: #1E1E1E;
				line-height: 30rpx;

				&__nocheck {
					width: 28rpx;
					height: 28rpx;
					background: #FFFFFF;
					border-radius: 5rpx 5rpx 5rpx 5rpx;
					border: 1rpx solid #BFBEC6;
					flex-shrink: 0;
				}

				&__check {
					width: 28rpx;
					height: 28rpx;
					background: #2a51fa;
					border-radius: 5rpx 5rpx 5rpx 5rpx;
					border: 1rpx solid #2a51fa;
					flex-shrink: 0;
					display: flex;
					align-items: center;
					justify-content: center;
				}
			}
		}
	}

	::v-deep.u-textarea {
		background-color: #F8F8F8 !important;
		border-radius: 20rpx !important;

	}

	::v-deep.u-textarea__field {
		color: #1E1E1E !important;
	}

	.btn {
		width: 638rpx;
		height: 95rpx;
		background: #2A51FA;
		border-radius: 20rpx 20rpx 20rpx 20rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		font-family: PingFang-SC, PingFang-SC;
		font-weight: 500;
		font-size: 30rpx;
		color: #FFFFFF;
		line-height: 48rpx;
		margin-top: 36rpx;
	}
</style>